package com.todo.sight.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.sight.application.base.apollo.SightApollo;
import com.todo.sight.application.base.apollo.entity.HotSightModel;
import com.todo.sight.application.convert.SightInfoConvert;
import com.todo.sight.application.entity.bo.*;
import com.todo.sight.application.frontend.service.SightPortalAggService;
import com.todo.sight.application.iface.frontend.request.RecommendSightParam;
import com.todo.sight.application.iface.frontend.request.SearchSightPoiParam;
import com.todo.sight.application.iface.frontend.request.SearchSightSummaryParam;
import com.todo.sight.application.iface.frontend.response.SightInfoVO;
import com.todo.sight.application.iface.frontend.response.SightSummaryVO;
import com.todo.sight.application.service.SightPoiService;
import com.todo.sight.application.service.SightService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 景点门户信息前台接口
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:48
 */
@Slf4j
@Service("sightPortalAggService")
public class SightPortalAggServiceImpl implements SightPortalAggService {

    @Resource
    private SightApollo sightApollo;
    @Resource
    private SightService sightService;
    @Resource
    private SightPoiService sightPoiService;


    @Override
    public BizResponse<List<SightSummaryVO>> hotSightListByCity(RecommendSightParam param) {
        if (param == null || !param.baseCheck()) {
            log.error("SightPortalAggService#hotSightListByCity 根据城市获取热门景点失败，基础数据校验为空，默认返回成功 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.success();
        }
        HotSightModel hotSightModel = sightApollo.getHotSightListByCity(param.getCity());
        if (hotSightModel == null) {
            log.info("SightPortalAggService#hotSightListByCity 根据城市查询配置数据完成，数据为空，数据未配置，默认返回成功 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.success();
        }
        if (CollectionUtils.isEmpty(hotSightModel.getSightList())) {
            log.info("SightPortalAggService#hotSightListByCity 根据城市查询配置数据完成，sightList为空，数据未配置，默认返回成功 入参：【{}】", JSONObject.toJSONString(param));
            return BizResponse.success();
        }
        List<Long> poiIdList = hotSightModel.getSightList().stream().map(HotSightModel.SightSimplyConf::getSightId).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(poiIdList)) {
            log.error("SightPortalAggService#hotSightListByCity 根据城市查询配置数据完成，数据配置异常，默认返回成功 入参：【{}】，hotSightModel：【{}】", JSONObject.toJSONString(param), JSONObject.toJSONString(hotSightModel));
            return BizResponse.success();
        }

        List<SightBaseBO> sightBaseBOList = sightService.querySightInfoByPoiIdList(poiIdList);
        if (CollectionUtils.isEmpty(sightBaseBOList)) {
            log.warn("SightPortalAggService#hotSightListByCity 根据景点Id列表查询完成，数据为空，默认返回成功 入参：【{}】", JSONObject.toJSONString(poiIdList));
            return BizResponse.success();
        }
        List<SightPoiBO> sightPoiBOList = sightPoiService.querySightPoiByIdList(poiIdList);
        return BizResponse.success(SightInfoConvert.buildSightSummaryVOList(sightBaseBOList, sightPoiBOList));
    }

    @Override
    public BizPageResponse<SightInfoVO> searchSightPOI(SearchSightPoiParam param) {
        if (param == null || !param.baseCheck()) {
            return BizPageResponse.success();
        }
        SearchSightPoiBO searchSightPoiBO = SightInfoConvert.convertToSearchSightPoiBO(param);
        PageResult<SearchSightPoiDetailBO> pageResult = sightService.searchSightPoi(searchSightPoiBO);
        return BizPageResponse.success(SightInfoConvert.convertToSightInfoVOPageResult(pageResult));
    }

    @Override
    public BizPageResponse<SightSummaryVO> searchSightSummary(SearchSightSummaryParam param) {
        if (param == null || !param.baseCheck()) {
            return BizPageResponse.success();
        }
        SearchSightSummaryBO searchSightSummaryBO = SightInfoConvert.convertToSearchSightSummaryBO(param);
        PageResult<SearchSightSummaryDetailBO> pageResult = sightService.searchSightSummary(searchSightSummaryBO);
        return BizPageResponse.success(SightInfoConvert.convertToSightSummaryVOPageResult(pageResult));
    }
}
